package com.panda.pandawash.util;
import android.util.Log;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.panda.pandawash.api.ApplicationConstant;
import com.panda.pandawash.bean.DeviceBean;
import com.panda.pandawash.bean.FeedbackBean;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.ParameterBean;
import com.panda.pandawash.bean.PromoteBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.bean.UserBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by gsoft2-3 on 16-6-13.
 */
public class HttpUtil {

    private static String TAG="API_TAG";
    public static final String URL_BASE="http://106.15.40.69:80/carwash/rest/";
    public static final String URL_REGISTE = URL_BASE +"activate_user/activate/";
    public static final String URL_USERINFO = URL_BASE +"activate_user/info/";
    public static final String URL_LOGIN = URL_BASE +"userlogin/login/";
    public static final String URL_LOGIN_IDCODE = URL_BASE +"activate_user/activate/withidcode/";
    public static final String URL_GET_IDCODE = URL_BASE +"identify/code/send";
    public static final String URL_DELETE = URL_BASE +"deactivate_user/deactivate/";
    public static final String URL_CHANGE_PSW = URL_BASE +"userpwd/reset";
    public static final String URL_CHANGE_PSW_FORCE = URL_BASE +"userpwd/reset/force";
    public static final String URL_CHANGE_PSW_FORCE_WITH_IDCODE = URL_BASE +"userpwd/reset/force/withidcode";
    public static final String URL_BIND_DEVICE = URL_BASE +"device/bind/newdevice";
    public static final String URL_DELETE_DEVICE = URL_BASE +"device/delete/device";
    public static final String URL_SET_PARAMETER_INFO = URL_BASE +"userparam/set";
    public static final String URL_UPDATE_PARAMETER_INFO = URL_BASE +"userparam/update";
    public static final String URL_IS_STRONGBOX_SETTED_PSW = URL_BASE +"strongbox/issetpwd";
    public static final String URL_STRONGBOX_SETUP_PSW = URL_BASE +"strongbox/setpwd";
    public static final String URL_STRONGBOX_LOGIN = URL_BASE +"strongbox/entry";
    public static final String URL_STRONGBOX_CHANGE_PSW = URL_BASE +"strongbox/resetpwd";
    public static final String URL_STRONGBOX_CHANGE_PSW_FORCE = URL_BASE +"strongbox/forceresetpwd";
    public static final String URL_STRONGBOX_CHANGE_PSW_FORCE_WITH_IDCODE = URL_BASE +"strongbox/forceresetpwd/withidcode";
    public static final String URL_FEEDBACK_SUBMIT = URL_BASE +"contactus/feedback/push";
    public static final String URL_FEEDBACK_GET = URL_BASE +"contactus/feedback/list/";
    public static final String URL_PROMOTE_SUBMIT = URL_BASE +"washshop/spread";


    private static ArrayList<Object> resultArray = null;

    public static void registe(AsyncHttpResponseHandler handler, String account, String username, String password){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_REGISTE  +account+"/"+account+"/"+password;
        Log.d(TAG,"registe url ="+url);
        client.get(url, handler);
    }


    public static void getUserInfo(AsyncHttpResponseHandler handler, String account, String username){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_USERINFO +account+"/"+account;
        Log.d(TAG,"userinfo url ="+url);
        client.get(url, handler);
    }

    public static void login(AsyncHttpResponseHandler handler, String account, String username, String password){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_LOGIN  +account+"/"+username+"/"+password;
        Log.d(TAG,"login url ="+url);
        client.get(url, handler);
    }

    public static void getIdCode(AsyncHttpResponseHandler handler, String account, String username){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put("useraccount",account);
        Log.d(TAG,"getIdCode url ="+URL_GET_IDCODE);
        Log.d(TAG,"useraccount ="+account);
        client.post(URL_GET_IDCODE,params, handler);
    }

    public static void loginWithIdCode(AsyncHttpResponseHandler handler, String account, String username, String password, String idCode){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_LOGIN_IDCODE  +account+"/"+username+"/"+password+"/"+idCode;
        Log.d(TAG,"loginwithidCode url ="+url);
        client.get(url, handler);
    }

    public static void delete(AsyncHttpResponseHandler handler, String account, String username, String password){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_DELETE  +account+"/"+account+"/"+password;
        Log.d(TAG,"delete url ="+url);
        client.get(url, handler);
    }

    public static void changePSW(AsyncHttpResponseHandler handler, String account, String username,String oldPsw, String newPsw){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.CHANGE_PASSWORD_USERNAME,account);
        params.put(ApplicationConstant.CHANGE_PASSWORD_USERACCOUNT,username);
        params.put(ApplicationConstant.CHANGE_PASSWORD_PASSWORD,oldPsw);
        params.put(ApplicationConstant.CHANGE_PASSWORD_NEWPASSOWRD,newPsw);
        Log.d(TAG,"URL_CHANGE_PSW ="+URL_CHANGE_PSW);
        client.post(URL_CHANGE_PSW, params, handler);
    }

    public static void forceChangePSW(AsyncHttpResponseHandler handler, String account,String username, String newpsw){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put("username",account);
        params.put("useraccount",account);
        params.put("newpassword",newpsw);
        Log.d(TAG,"URL_CHANGE_PSW_FORCE ="+URL_CHANGE_PSW_FORCE);
        client.post(URL_CHANGE_PSW_FORCE, params, handler);
    }

    public static void forceChangePSWWithIdCode(AsyncHttpResponseHandler handler, String account,String username, String newpsw,String idcode){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put("username",account);
        params.put("useraccount",account);
        params.put("newpassword",newpsw);
        params.put("idcode",idcode);
        Log.d(TAG,"params ="+params);
        Log.d(TAG,"URL_CHANGE_PSW_FORCE_WITH_IDCODE ="+URL_CHANGE_PSW_FORCE_WITH_IDCODE);
        client.post(URL_CHANGE_PSW_FORCE_WITH_IDCODE, params, handler);
    }

    public static void isSetedStrongPSW(AsyncHttpResponseHandler handler, String account, String username){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put("username",account);
        params.put("useraccount",account);

        Log.d(TAG,"URL_IS_STRONGBOX_SETTED_PSW ="+URL_IS_STRONGBOX_SETTED_PSW);
        client.post(URL_IS_STRONGBOX_SETTED_PSW, params, handler);
    }


    public static void setupStrongBoxPSW(AsyncHttpResponseHandler handler, String account, String username, StrongBoxBean sbb){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.STRONGBOX_USERNAME,account);
        params.put(ApplicationConstant.STRONGBOX_USERACCOUNT,username);
        params.put(ApplicationConstant.STRONGBOX_PASSWORD,sbb.getSecretpassword());

        Log.d(TAG,"URL_STRONGBOX_SETUP_PSW ="+URL_STRONGBOX_SETUP_PSW);
        client.post(URL_STRONGBOX_SETUP_PSW, params, handler);
    }

    public static void loginStrongBoxPSW(AsyncHttpResponseHandler handler,String account,String username,StrongBoxBean sbb) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.STRONGBOX_USERNAME, account);
        params.put(ApplicationConstant.STRONGBOX_USERACCOUNT, username);
        params.put(ApplicationConstant.STRONGBOX_PASSWORD, sbb.getSecretpassword());

        Log.d(TAG, "login Strong box url =" + URL_STRONGBOX_LOGIN);
        Log.d(TAG, "params =" + params);
        client.post(URL_STRONGBOX_LOGIN, params, handler);
    }

    public static void changeStrongBoxPSW(AsyncHttpResponseHandler handler, String account, String username, StrongBoxBean sbb){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.STRONGBOX_USERNAME,account);
        params.put(ApplicationConstant.STRONGBOX_USERACCOUNT,account);
        params.put(ApplicationConstant.STRONGBOX_PASSWORD,sbb.getSecretpassword());
        params.put(ApplicationConstant.STRONGBOX_NEWPASSWORD,sbb.getNewsecretpassword());

        Log.d(TAG,"URL_STRONGBOX_CHANGE_PSW ="+URL_STRONGBOX_CHANGE_PSW);
        client.post(URL_STRONGBOX_CHANGE_PSW, params, handler);
    }

    public static void forceChangeStrongBoxPSW(AsyncHttpResponseHandler handler, String account, String username, StrongBoxBean sbb){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.STRONGBOX_USERNAME,account);
        params.put(ApplicationConstant.STRONGBOX_USERACCOUNT,username);
        params.put(ApplicationConstant.STRONGBOX_NEWPASSWORD,sbb.getNewsecretpassword());

        Log.d(TAG,"URL_STRONGBOX_CHANGE_PSW_FORCE ="+URL_STRONGBOX_CHANGE_PSW_FORCE);
        client.post(URL_STRONGBOX_CHANGE_PSW_FORCE, params, handler);
    }

    public static void forceChangeStrongBoxPSWWithIdCode(AsyncHttpResponseHandler handler, String account, String username, StrongBoxBean sbb,String idCode){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.STRONGBOX_USERNAME,account);
        params.put(ApplicationConstant.STRONGBOX_USERACCOUNT,username);
        params.put(ApplicationConstant.STRONGBOX_NEWPASSWORD,sbb.getNewsecretpassword());
        params.put("idcode",idCode);

        Log.d(TAG,"params ="+params);
        Log.d(TAG,"URL_STRONGBOX_CHANGE_PSW_FORCE_WITH_IDCODE ="+URL_STRONGBOX_CHANGE_PSW_FORCE_WITH_IDCODE);
        client.post(URL_STRONGBOX_CHANGE_PSW_FORCE_WITH_IDCODE, params, handler);
    }



    public static void feedbackSubmit(AsyncHttpResponseHandler handler, String account, String username, FeedbackBean fbb){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.FEEDBACK_USERNAME,username);
        params.put(ApplicationConstant.FEEDBACK_USERACCOUNT,account);
        params.put(ApplicationConstant.FEEDBACK_TITLE,fbb.getTitle());
        params.put(ApplicationConstant.FEEDBACK_CONTENT,fbb.getContent());
        params.put(ApplicationConstant.FEEDBACK_DATE,fbb.getDate());
        Log.d(TAG,"URL_FEEDBACK_SUBMIT ="+URL_FEEDBACK_SUBMIT);
        client.post(URL_FEEDBACK_SUBMIT, params, handler);
    }

    public static void getFeedbackInfo(AsyncHttpResponseHandler handler, String account, String username){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        String url = URL_FEEDBACK_GET +username+"/"+account;
        Log.d(TAG,"url ="+url);
        client.get(url, handler);
    }

    public static void promoteSubmit(AsyncHttpResponseHandler handler, String account, String username, PromoteBean pb){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.PROMOTE_USERNAME,username);
        params.put(ApplicationConstant.PROMOTE_USERACCOUNT,account);
        params.put(ApplicationConstant.PROMOTE_SHOPNAME,pb.getShopname());
        params.put(ApplicationConstant.PROMOTE_LONGTITUDE,pb.getLongtitude());
        params.put(ApplicationConstant.PROMOTE_LATITUDE,pb.getLatitude());
        params.put(ApplicationConstant.PROMOTE_DESCRIPTION,pb.getDescription());
        Log.d(TAG,"URL_PROMOTE_SUBMIT ="+URL_PROMOTE_SUBMIT);
        client.post(URL_PROMOTE_SUBMIT, params, handler);
    }


    public static void bindDevice(AsyncHttpResponseHandler handler, String account, String username, DeviceBean deviceBean){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.BIND_DEVICE_USERNAME, username);
        params.put(ApplicationConstant.BIND_DEVICE_USERACCOUNT, account);
        params.put(ApplicationConstant.BIND_DEVICE_DEVICESN, deviceBean.getDevicesn());
        params.put(ApplicationConstant.BIND_DEVICE_DEVICENAME, deviceBean.getDevicename());
        params.put(ApplicationConstant.BIND_DEVICE_MANUFACTURER, deviceBean.getManufacturer());
        params.put(ApplicationConstant.BIND_DEVICE_CERTIFIED, deviceBean.isCertified());
        params.put(ApplicationConstant.BIND_DEVICE_MODE, deviceBean.getMode());
        params.put(ApplicationConstant.BIND_DEVICE_SWVERSION, deviceBean.getSwversion());
        params.put(ApplicationConstant.BIND_DEVICE_DESC, deviceBean.getDescription());
        Log.d(TAG,"URL_BIND_DEVICE ="+URL_BIND_DEVICE);
        client.post(URL_BIND_DEVICE, params, handler);
    }

    public static void deleteDevice(AsyncHttpResponseHandler handler, String account, String username, DeviceBean deviceBean){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.DELETE_DEVICE_USERNAME, account);
        params.put(ApplicationConstant.DELETE_DEVICE_USERACCOUNT, username);
        params.put(ApplicationConstant.DELETE_DEVICE_DEVICESN, deviceBean.getDevicesn());
        Log.d(TAG,"URL_DELETE_DEVICE ="+URL_DELETE_DEVICE);
        client.post(URL_DELETE_DEVICE, params, handler);
    }

    public static void setParameterInfo(AsyncHttpResponseHandler handler, String account, String username, ParameterBean paraBean){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.PARAMETERBEAN_USERNAME, username);
        params.put(ApplicationConstant.PARAMETERBEAN_USERACCOUNT, account);
        params.put(ApplicationConstant.PARAMETERBEAN_ELECTRICPRICE, paraBean.getElectricprice());
        params.put(ApplicationConstant.PARAMETERBEAN_WATERPRICE, paraBean.getWaterprice());
        params.put(ApplicationConstant.PARAMETERBEAN_ROOMRENT, paraBean.getRoomrent());
        params.put(ApplicationConstant.PARAMETERBEAN_EMPLOYEESALARY, paraBean.getEmployeesalary());
        params.put(ApplicationConstant.PARAMETERBEAN_WASHPRICE, paraBean.getWashprice());
        params.put(ApplicationConstant.PARAMETERBEAN_DISCOUNTTIME, paraBean.getDiscounttime());

        Log.d(TAG,"URL_SET_PARAMETER_INFO ="+URL_SET_PARAMETER_INFO);
        client.post(URL_SET_PARAMETER_INFO, params, handler);
    }

    public static void updateParameterInfo(AsyncHttpResponseHandler handler, String account, String username, ParameterBean paraBean){
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(5000);
        RequestParams params = new RequestParams();
        params.put(ApplicationConstant.PARAMETERBEAN_USERNAME, username);
        params.put(ApplicationConstant.PARAMETERBEAN_USERACCOUNT, account);
        params.put(ApplicationConstant.PARAMETERBEAN_ELECTRICPRICE, paraBean.getElectricprice());
        params.put(ApplicationConstant.PARAMETERBEAN_WATERPRICE, paraBean.getWaterprice());
        params.put(ApplicationConstant.PARAMETERBEAN_ROOMRENT, paraBean.getRoomrent());
        params.put(ApplicationConstant.PARAMETERBEAN_EMPLOYEESALARY, paraBean.getEmployeesalary());
        params.put(ApplicationConstant.PARAMETERBEAN_WASHPRICE, paraBean.getWashprice());
        params.put(ApplicationConstant.PARAMETERBEAN_DISCOUNTTIME, paraBean.getDiscounttime());

        Log.d(TAG,"URL_UPDATE_PARAMETER_INFO ="+URL_UPDATE_PARAMETER_INFO);
        client.post(URL_UPDATE_PARAMETER_INFO, params, handler);
    }

    public static HttpResultBean parserHttpResultInfo(String content, String type){
        HttpResultBean resultBean=new HttpResultBean();
        try {
            JSONObject jsonObject = new JSONObject(content);
            String code = jsonObject.getString(ApplicationConstant.HTTP_RESULT_BEAN_CODE);
            String message = jsonObject.getString(ApplicationConstant.HTTP_RESULT_BEAN_MESSAGE);

            resultBean.setCode(code);
            resultBean.setMessage(message);

            if(code.equals("0")){
                Log.d(TAG,"type = "+type);
                if(type == null){
                    String data = null;
                    try {
                        data = jsonObject.getString(ApplicationConstant.HTTP_RESULT_BEAN_DATA);
                    }catch (JSONException e){
                        resultBean.setData(null);
                        Log.d(TAG,"type = null JSONException = "+e);
                        return resultBean;
                    }
                    resultArray = new ArrayList<Object>();
                    resultArray.add(data);
                    Log.d(TAG,"resultArray = "+resultArray);
                    resultBean.setData(resultArray);
                    return resultBean;
                }else{
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = jsonObject.getJSONArray(ApplicationConstant.HTTP_RESULT_BEAN_DATA);

                    }catch (JSONException e){
                        resultBean.setData(null);
                        Log.d(TAG,"JSONException = "+e);
                        return resultBean;
                    }
                    resultArray = new ArrayList<Object>();
                    for (int i= 0;i<jsonArray.length();i++) {
                        JSONObject jsonObjectSon = (JSONObject) jsonArray.opt(i);
                        Object object = null;
                        if(type.equals("user")){
                            object = new UserBean();
                            ((UserBean)object).setUsername(jsonObjectSon.getString(ApplicationConstant.USERBEEN_NAME));
                            ((UserBean)object).setUseraccount(jsonObjectSon.getString(ApplicationConstant.USERBEEN_ACCOUNT));
                            ((UserBean)object).setPhotoUrl(jsonObjectSon.getString(ApplicationConstant.USERBEEN_PHOTO_URL));
                            ((UserBean)object).setActivateDate(jsonObjectSon.getString(ApplicationConstant.USERBEEN_ACTIVATE_DATE));
                            ((UserBean)object).setDescription(jsonObjectSon.getString(ApplicationConstant.USERBEEN_DESC));
                            ((UserBean)object).setEmail(jsonObjectSon.getString(ApplicationConstant.USERBEEN_EMAIL));
                        }else if (type.equals("feedback")){
                            JSONObject jsonObjectSonFeedback = new JSONObject(jsonObjectSon.getString("id"));
                            object = new FeedbackBean();
                            ((FeedbackBean)object).setTitle(jsonObjectSonFeedback.getString(ApplicationConstant.FEEDBACK_TITLE));
                            ((FeedbackBean)object).setContent(jsonObjectSonFeedback.getString(ApplicationConstant.FEEDBACK_CONTENT));
                            ((FeedbackBean)object).setDate(jsonObjectSonFeedback.getString(ApplicationConstant.FEEDBACK_DATE));

                        }else{

                        }

                        resultArray.add(object);
                    }
                    resultBean.setData(resultArray);
                }
            }else if(code.equals("99")){
                resultBean.setData(null);
            }

            return resultBean;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
